package com.xunqi.crm.web.controller;

import com.xunqi.crm.pojo.User;
import com.xunqi.crm.service.UserService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Controller
public class LoginController {

    @Resource
    private UserService userService;

    @GetMapping(value = {"/","/login"})
    public String login(){
        return "login";
    }

    @GetMapping(value = "/main")
    public String main(){
        return "main";
    }

    @PostMapping(value = "/dologin")
    public String doLogin(String usrName,String usrPassword, HttpServletRequest request){
        User user = userService.login(usrName,usrPassword);
        if(user!=null){
            request.getSession().setAttribute("user",user);
            return "redirect:/main";
        }else{
            request.setAttribute("msg","登录失败，用户名或密码错误！");
            return "login";
        }
    }
}
